<template>
	<view>
		<form>
			<view class="cu-form-group margin-top">
				<view class="title">用户名</view>
				<input disabled v-model="ownerInfo.ownerName"  name="ownerName"></input>
			</view>
			
			<view class="cu-form-group">
				<view class="title">手机号码</view>
				<input disabled v-model="ownerInfo.ownerName"  name="ownerTel" ></input>
				<view class="cu-capsule radius">
					<view class='cu-tag bg-blue '>
						+86
					</view>
					<view class="cu-tag line-blue">
						中国大陆
					</view>
				</view>
			</view>
			
			<radio-group class="block" @change="RadioChange">
				<view class="cu-form-group">
					<view class="title">性别</view>
					<!-- <view class="title">{{ownerInfo.ownerSex}}</view> -->
					<view>
						<radio v-show="ownerInfo.ownerSex=='男'" class='blue radio' :class="true?'checked':''" :checked="ownerInfo.ownerSex=='男'"  >男</radio>
						<radio v-show="ownerInfo.ownerSex=='女'" class='red margin-left-sm' :class="true?'checked':''" :checked="ownerInfo.ownerSex=='女'" >女</radio>
					</view>
				</view>
			</radio-group>
			
			<view class="cu-bar bg-white margin-top">
				<view class="action">
					更换头像
				</view>
				<view class="action">
					{{imgList.length}}/1
				</view>
			</view>
			<view class="cu-form-group">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					 <image :src="imgList[index]" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage" v-if="imgList.length<1">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>
		</form>
		<view class="register-commit">
			<button @click="updateProfile" class='cu-btn bg-green shadow'>确认修改</button>
		</view>
	</view>
</template>

<script>
	import { mapMutations } from 'vuex';
	export default {
		data() {
			return {
				index: -1,
				multiIndex: [0, 0, 0],
				time: '12:01',
				date: '2018-12-25',
				switchA: false,
				switchB: true,
				switchC: false,
				switchD: false,
				radio: 'A',
				checkbox: [{
					value: 'A',
					checked: true
				}, {
					value: 'B',
					checked: true
				}, {
					value: 'C',
					checked: false
				}],
				imgList: [],
				modalName: null,
				textareaAValue: '',
				textareaBValue: '',
				ownerInfo:{},
				tempPath:'',
			};
		},
		onLoad() {
			//获取业主信息并填充
			this.ownerInfo=this.$store.state.userInfo
		},
		methods: {
			PickerChange(e) {
						this.index = e.detail.value
					},
					MultiChange(e) {
						this.multiIndex = e.detail.value
					},
					TimeChange(e) {
						this.time = e.detail.value
					},
					DateChange(e) {
						this.date = e.detail.value
					},
					RegionChange(e) {
						this.region = e.detail.value
					},
					SwitchA(e) {
						this.switchA = e.detail.value
					},
					SwitchB(e) {
						this.switchB = e.detail.value
					},
					SwitchC(e) {
						this.switchC = e.detail.value
					},
					SwitchD(e) {
						this.switchD = e.detail.value
					},
					RadioChange(e) {
						this.radio = e.detail.value
					},
					...mapMutations(['changeIcon']),
					updateProfile(){
						//判断是否选择了头像
						if(this.imgList[0]){
							let filePath = this.imgList[0];
							if(filePath){
								uni.uploadFile({
									url: this.baseurl + '/owner/icon',
									filePath: filePath,
									name: 'da',
									formData:{
										ownerId:this.ownerInfo.ownerId
									},
									success: res => {
										uni.showToast({
											icon:'success',
											title:'修改成功'
										})
										setTimeout(()=>{
											let obj=JSON.parse(res.data)
											//修改头像
											this.changeIcon(obj.data);
											//返回到前个页面
											uni.navigateBack()
										},1000)
									},
								})
							}
						}else{
							uni.showToast({
								icon:'none',
								title:'请选择合适的头像'
							})
						}
					},
					CheckboxChange(e) {
						var items = this.checkbox,
							values = e.detail.value;
						for (var i = 0, lenI = items.length; i < lenI; ++i) {
							items[i].checked = false;
							for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
								if (items[i].value == values[j]) {
									items[i].checked = true;
									break
								}
							}
						}
					},
					ChooseImage() {
						uni.chooseImage({
							count: 4, //默认9
							sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
							sourceType: ['album'], //从相册选择
							success: (res) => {
								if (this.imgList.length != 0) {
									this.imgList = this.imgList.concat(res.tempFilePaths)
								} else {
									this.imgList = res.tempFilePaths
								}
							}
						});
					},
					ViewImage(e) {
						uni.previewImage({
							urls: this.imgList,
							current: e.currentTarget.dataset.url
						});
					},
					DelImg(e) {
						uni.showModal({
							title: '头像',
							content: '确定要删除选择的头像吗？',
							cancelText: '取消',
							confirmText: '确认删除',
							success: res => {
								if (res.confirm) {
									this.imgList.splice(e.currentTarget.dataset.index, 1)
								}
							}
						})
					},
					textareaAInput(e) {
						this.textareaAValue = e.detail.value
					},
					textareaBInput(e) {
						this.textareaBValue = e.detail.value
					}
				}
			}
</script>

<style>
	@import url("../public/main.css");
	@import url("../public/icon.css");

	.cu-form-group .title {
		min-width: calc(4em + 15px) ;
	}
</style>
